package com.example.sql;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.yml.R;

public class SQLOpenHelperActivity extends AppCompatActivity implements View.OnClickListener {

    private Button tj;
    private Button sc;
    private Button xg;
    private Button cx;
    private MyOpenHelper myOpenHelper;
    private EditText et_name;
    private EditText et_pass;
    private Button button_xz;
    private LinearLayout ll_tj;
    private EditText et1;
    private EditText et2;
    private EditText et3;
    private LinearLayout ll_xg;
    private Button button_xg;
    private EditText et_id;
    private Button butt_sc;
    private LinearLayout ll_sc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlopen_helper);
        initView();
        myOpenHelper = new MyOpenHelper(this);
    }

    private void initView() {
        tj = (Button) findViewById(R.id.tj);
        sc = (Button) findViewById(R.id.sc);
        xg = (Button) findViewById(R.id.xg);
        cx = (Button) findViewById(R.id.cx);

        tj.setOnClickListener(this);
        sc.setOnClickListener(this);
        xg.setOnClickListener(this);
        cx.setOnClickListener(this);
        et_name = (EditText) findViewById(R.id.et_name);
        et_name.setOnClickListener(this);
        et_pass = (EditText) findViewById(R.id.et_pass);
        et_pass.setOnClickListener(this);
        button_xz = (Button) findViewById(R.id.button_xz);
        button_xz.setOnClickListener(this);
        ll_tj = (LinearLayout) findViewById(R.id.ll_tj);
        ll_tj.setOnClickListener(this);
        et1 = (EditText) findViewById(R.id.et1);
        et1.setOnClickListener(this);
        et2 = (EditText) findViewById(R.id.et2);
        et2.setOnClickListener(this);
        et3 = (EditText) findViewById(R.id.et3);
        et3.setOnClickListener(this);
        ll_xg = (LinearLayout) findViewById(R.id.ll_xg);
        ll_xg.setOnClickListener(this);
        button_xg = (Button) findViewById(R.id.button_xg);
        button_xg.setOnClickListener(this);
        et_id = (EditText) findViewById(R.id.et_id);
        et_id.setOnClickListener(this);
        butt_sc = (Button) findViewById(R.id.butt_sc);
        butt_sc.setOnClickListener(this);
        ll_sc = (LinearLayout) findViewById(R.id.ll_sc);
        ll_sc.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tj://
                ll_tj.setVisibility(View.VISIBLE);
                break;
            case R.id.button_xz://添加
                String name1 = et_name.getText().toString().trim();
                String pass1 = et_pass.getText().toString().trim();
                ContentValues values = new ContentValues();
                values.put("username", name1);
                values.put("pass", pass1);
                SQLiteDatabase writableDatabase = myOpenHelper.getWritableDatabase();
                long info = writableDatabase.insert("info", null, values);
                writableDatabase.close();
                ll_tj.setVisibility(View.GONE);
                Log.e("onClick: ", info + "");
                break;
            case R.id.sc://删除
                ll_sc.setVisibility(View.VISIBLE);
                break;
            case R.id.butt_sc:
                SQLiteDatabase writableDatabase2 = myOpenHelper.getWritableDatabase();
                String _id = et_id.getText().toString().trim();
                int info2 = writableDatabase2.delete("info", "_id=?", new String[]{_id});
                ll_sc.setVisibility(View.GONE);
                break;
            case R.id.xg:
                ll_xg.setVisibility(View.VISIBLE);
                break;
            case R.id.button_xg://修改
                SQLiteDatabase writableDatabase1 = myOpenHelper.getWritableDatabase();
                ContentValues values1 = new ContentValues();
                String xg_username = et1.getText().toString().trim();
                String xg_lm = et2.getText().toString().trim();
                String xg = et3.getText().toString().trim();
                values1.put(xg_lm, xg);
                int info1 = writableDatabase1.update("info", values1, "username=?", new String[]{xg_username});
                ll_xg.setVisibility(View.GONE);
                writableDatabase1.close();
                break;
            case R.id.cx://查询
                SQLiteDatabase readableDatabase = myOpenHelper.getReadableDatabase();
                /*
                 * 参数1 要查询的表名；
                 * 参数2 要查询的列（数组类型，null为查询所有列）
                 * 参数3 要查询的条件
                 * 参数4 给查询条件赋值
                 * 参数5 分组
                 * 参数6 过滤条件
                 * 参数7 排序
                 * cursor 游标
                 * */
                Cursor cursor = readableDatabase.query("info", null, null, null, null, null, null);
                if (cursor != null && cursor.getCount() > 0) {
                    while (cursor.moveToNext()) {
                        int id = cursor.getInt(0);
                        String name = cursor.getString(1);
                        String pass = cursor.getString(2);
                        user user = new user();
                        user.setName(name);
                        user.setPass(pass);
                        String TAG = "SF";
                        Log.e(TAG, "onClick: " + user.toString());
                        Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "查询失败", Toast.LENGTH_SHORT).show();
                }
                cursor.close();
                readableDatabase.close();
                break;

        }
    }
}
